#include <stdio.h>
#include <string.h>
#include <stdlib.h>


#include "constantes.h"
#include "estructuras.h"
#include "sockets.h"
#include "NIPC.h"


int generarStructIPC(char *payload, short mode, InfoIPC *rInfoIPC)  /*sort es 2 bytes,int es 4 bytes,uso short porq no es necesario usar mas en este caso*/
{	
	strcpy(rInfoIPC->descriptorID,GenerarDescriptor()); /*creo el descriptor y lo cargo en la estructura. */
	rInfoIPC->payLoadDescriptor=mode; /*cargo el protocolo a la struct*/		
	rInfoIPC->payLoadLength = strlen(payload) +1;	
	rInfoIPC->payload = (char*) malloc(rInfoIPC->payLoadLength);
	memset(rInfoIPC->payload,'\0',rInfoIPC->payLoadLength);
	memcpy(rInfoIPC->payload,payload,rInfoIPC->payLoadLength); 


	return 0; 
}


char* GenerarDescriptor(){

	static char descriptor[16];
	char cadena [] = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	int pos = 0;

 
	memset(descriptor, '\0', 16);

	for(pos = 0; pos < 15; descriptor[pos++] = cadena[(int)(rand()%62)]);
	descriptor[pos] = '\0';

	return descriptor;
}


int enviarIPC(int sock, InfoIPC *rInfoIPC)
{
	int tm;
	char *buffer;
	int bufLen;
	int length;

	bufLen = (sizeof(char) * strlen(rInfoIPC->descriptorID)) + sizeof(short) + sizeof(long) + sizeof(char)*rInfoIPC->payLoadLength + 20; /*Ese 20 no se de donde sale pero por las dudas lo dejo*/
	
	buffer	= (char*)malloc(bufLen);
	length =0;
	memcpy (buffer,&(rInfoIPC->payLoadLength),sizeof(long));
	length = sizeof(long);
	memcpy(buffer + length, &(rInfoIPC->payLoadDescriptor), sizeof(short));
	length += sizeof(short);
	memcpy(buffer + length, rInfoIPC->descriptorID, strlen(rInfoIPC->descriptorID)+1);
	length += strlen(rInfoIPC->descriptorID)+1;
	memcpy(buffer + length, rInfoIPC->payload, rInfoIPC->payLoadLength);
	length +=  rInfoIPC->payLoadLength; 
	
	tm = send(sock,buffer,length,0);
	return tm;
}


int recibirIPC(int sock, InfoIPC* rInfoIPC)
{
	int length;
	int len;
	char* buffer;
	char* payload;
	int ret;

	len = sizeof(short) + sizeof(long) + 16; /*tamaño del header, sin payload.*/

	buffer	= (char*) malloc(len);

	ret = recv(sock, buffer, len, 0);
	rInfoIPC->payLoadLength = 0;
	if(ret == len)
	{   
		length  = 0;
		rInfoIPC->payLoadLength =  *((long *) buffer);
		length += sizeof(long);

		rInfoIPC->payLoadDescriptor = *((short*) (buffer + length));
		length += sizeof(short);

		strncpy(rInfoIPC->descriptorID,(char*)( buffer + length),16);
		length += 16;
	}else return -1; /*retorno NOSERECIBIOLACANTDEDATOSAPROPIADA*/
	
	if(rInfoIPC->payLoadLength != 0)
	{
     
         payload	= (char*)malloc(rInfoIPC->payLoadLength);
     		
      /*recibirDatos(sock, payload, rInfoIPC->payLoadLength);*/
      ret = recv(sock, payload,  rInfoIPC->payLoadLength, 0);   /*fuerzo a recibir dos veces para que en la segunda reciba los datos(pero es un solo send del otro lado)*/	
  
         rInfoIPC->payload 	= (char*)malloc(rInfoIPC->payLoadLength);
     	
      memset(rInfoIPC->payload,'\0',rInfoIPC->payLoadLength);
		memcpy(rInfoIPC->payload,payload,rInfoIPC->payLoadLength);
		return 0;  /*retorno OK*/

   }else return HANDSHAKE;	
}	

void instrucciones(void){

   printf("\nMENU CONSOLA PPD \n");
   printf("------------------\n");
   
   printf("Ingrese alguno de los siguientes comandos\n\n");
   printf("1)trace S1 [S2 S3 S4 S5] \n");
   printf("2)info\n");
   printf("3)clean S1 S2\n");
   printf("4)read\n");
   printf("5)write\n");
   
   printf("\n");
   
 
}






















